<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class user extends REST_Controller


{
	function signup_put()
	{
		if(
		!$this->put('user_account_name') ||
		!$this->put('user_password') ||
		!$this->put('user_name') ||
		!$this->put('user_surname') ||
		!$this->put('default_neighborhood_id') ||
		!$this->put('user_email') ||
		!$this->put('user_phone') ||
		!$this->put('branch_phone') ||
		!$this->put('role_id') ||
		!$this->put('street_id') ||
		!$this->put('remaining_address')
		)
		{
			//$this->response(array('status'=>"false"), 200); // 200 being the HTTP response code
			//$this->response(array('status'=>"falseEEEE"), 200); // 200 being the HTTP response code
			$this->response(
				$this->put('user_account_name') .
			 	$this->put('user_password') .
				$this->put('user_name') .
				$this->put('user_surname') .
				$this->put('user_email') .
				$this->put('user_phone') .
				$this->put('user_birthdate') .
				$this->put('user_is_male') .
				$this->put('role_id').
				$this->put('street_id').
				$this->put('remaining_address').
				$this->put('branch_phone'),200);
		}
		$this->load->model('userdao');
		$userresult = $this->userdao->signup(
				$this->put('user_account_name') ,
			 	$this->put('user_password') ,
				$this->put('user_name') ,
				$this->put('user_surname') ,
				$this->put('default_neighborhood_id') ,
				$this->put('user_email') ,
				$this->put('user_phone') ,
				$this->put('user_birthdate') ,
				$this->put('user_is_male') ,
				$this->put('role_id'),
				$this->put('street_id'),
				$this->put('remaining_address'),
				$this->put('branch_phone')
		);
		if(!$userresult){
			$this->response(array('status'=>"false"), 200); // 200 being the HTTP response code
		} else {
			$this->response(array('result'=>$userresult,'status'=>"true"), 200); // 200 being the HTTP response code
		}
	}/*
	
			$user_account_name,
			$user_password,
			$user_name,
			$user_surname,
			$default_neighborhood,
			$user_email,
			$user_phone,
			$user_birthdate,
			$ismale,
			$role_id,
			$street_id,
			$remaining_address,
			$branch_phone
	*
	*/
//signup nerde???
		function getbyid_get()
		{
			if(!$this->get('id'))
			{
				$this->response(NULL, 400);
			}
			$this->load->model('userdao');
			$user = $this->userdao->getbyid($this->get('id'));
			$this->response($user, 200); // 200 being the HTTP response code
		}

		function getusers_get()
		{
			$this->load->model('userdao');
			$user = $this->userdao->getusers();
			$this->response($user, 200); // 200 being the HTTP response code
		}

		function insert_put()
		{
			if(
			!$this->put('user_account_name') ||
			!$this->put('user_password') ||
			!$this->put('user_name') ||
			!$this->put('user_surname') ||
			!$this->put('user_email') 
			)
			{
				$this->response(array('status'=>"asdf"), 200);
			}
			$this->load->model('userdao');
			$user = $this->userdao->insertuser(
					$this->put('user_account_name'),
					$this->put('user_password'),
					$this->put('user_name'),
					$this->put('user_surname'),
					$this->put('default_neighborhood_id'),
					$this->put('user_email'),
					$this->put('user_phone'),
					$this->put('user_birthdate'),
					$this->put('ismale')
			);
			if($user!=0){
			$this->response(array('markets'=>$user,'status'=> $user ? 'true' : 'false'), 200);
			} else {
				$this->response(array('status'=>"false"), 200); // 200 being the HTTP response code
			}
		}
	
	
	function login_get()
	{
		if(!$this->get('username') || !$this->get('password'))
		{
			$this->response(array('user' => '0','status' => 'false','message'=>'username or password loss'), 200); // 200 being the HTTP response code
		}
		$this->load->model('userdao');
		$loginResult = $this->userdao->login($this->get('username'),$this->get('password'));
		if($loginResult){
			$message = array('user' => $loginResult,'status' => 'success');
			$this->response($message,200);
		} else {
			$message = array('user' => '0','status' => 'false','message'=>'username or password invalid'); // 200 being the HTTP response code
			$this->response($message,200);
		}
	}
}

/*
 * 
 * 	public function signup(
			$user_account_name,
			$user_password,
			$user_name,
			$user_surname,
			$default_neighborhood,
			$user_email,
			$user_phone,
			$user_birthdate,
			$ismale,
			$role_id
			){
		$this->load->model('userdao');
		$user_result = $this->userdao->insertuser(
				$user_account_name,
				$user_password,
				$user_name,
				$user_surname,
				$default_neighborhood,
				$user_email,
				$user_phone,
				$user_birthdate,
				$ismale
		);
		if(!$user_result)
		{
			return false;
		}
		else {
			$return_id = $this->db->insert_id();
			array_push($data,
			array('user_id' => $return_id,  'status' => 'false' or 'success'));
			$this->load->model('userhasroledao');
			$role_result = $this->userhasroledao->insert_role_to_user($user_id,$role_id);
			if(!$role_result){
				return false;
			}
			return $data;
		}
	}
 * */